home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Unix / mcvert2.15.shar / mcvert.c < prev    next >
Encoding:
Text File  |  1993-11-25  |  43.5 KB  |  1,405 lines

  1. /**
  2.  * mcvert.c - version 1.05 - 10 January, 1990
  3.  * Written by Doug Moore - Rice University - dougm@rice.edu - April '87
  4.  
  5.  * Sun bug fixes, assorted stuff - Jim Sasaki, March '89
  6.  
  7.  * Changed default max_line_size from 2000 to unlimited -
  8.  *                                           Doug Moore, April, '89
  9.  
  10.  * Sun 3/60 doesn't like odd-sized structs.  Bug fixed - Doug Moore, April, '89
  11.  *                                              - aided by Spencer W. Thomas
  12.  
  13.  * Didn't handle properly many hqx files combined in one file.  Bug fixed -
  14.  *                                           Doug Moore, June, '89
  15.  
  16.  * Modified to handle MacBinaryII specification. Jim Van Verth, Sept, '89
  17.  
  18.  * Fixed a bug when there are blank lines in hqx data, as happens when newline
  19.  * get translated to CRLF and then to \n\n, common for some file transfers.
  20.  * The last hqx line would be lost if the previous line was blank or junk.
  21.  *    Glenn Trewitt, Stanford University, 1990    (1.05)
  22.  
  23.  * Fixed a bug that occurred when extracting data or resource
  24.  * forks on a Sun 4.  It was a byte alignment problem.
  25.  * Rick Zaccone, zaccone@bucknell.edu.  April 1991.  Version 1.6
  26.  
  27.  * Fixed:
  28.  *   Sent all "Converting ... " lines to stdout instead of stderr
  29.  *   Changed mactypes.h for HP-UX systems
  30.  *      Alan Danziger, aland@cs.brandeis.edu.  October 1991. Version 1.6.5
  31.  
  32.  * ----------------------------------------------------------------------------
  33.  * External
  34.  * -----
  35.  * Fixed buffering bug when converting very small MacBinary files to hqx files.
  36.  * Provide helpful usage line.
  37.  * Control "Converting ... " lines separately with -S flag.
  38.  * Make encoding and decoding consistent by ignoring locked and init flags.
  39.  * Clean up some error messages; check for more errors; provide errno on error.
  40.  * Updated the man page.
  41.  * -----
  42.  * Internal
  43.  * -----
  44.  * Reformat source (sorry, local standard used by tools is tab space == 3)
  45.  * Remove compiler warning messages.
  46.  * Rename some variables.
  47.  * Added some comments to code.
  48.  * Added some offsets to struct definitions.
  49.  * Since the makefile has compilation flags,
  50.  *    make the compiles depend on the Makefile.
  51.  * -----
  52.  * Thanks to all who have gone before for creating, maintaining,
  53.  * improving, and providing this program and documentation.
  54.  * -----
  55.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  56.  * {Jskud@std.mentorg.com,Joseph_Skudlarek@mentorg.com}
  57.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  58.  * Version 1.70 09Jul92
  59.  * ----------------------------------------------------------------------------
  60.  
  61.  * ----------------------------------------------------------------------------
  62.  * External
  63.  * -----
  64.  * Added -V (Verbose) option (includes debugging information).
  65.  * Fixed bug converting hqx to MacBinary if last line is ":".
  66.  * Avoided a silent error and quick exit situation.
  67.  * -----
  68.  * Internal
  69.  * -----
  70.  * Got rid of almost all lint (SunOS and HP-UX) error messages.
  71.  * Compiled on SunOs, HP-UX, DomainOS.
  72.  * Incorporated Parag Patel <parag@netcom.com> changes for AU/X.
  73.  *    Here's some diffs for really quick cheap hacks to get mcvert to compile
  74.  *    and run under A/UX.  The main problem was that timeb does not exist, so
  75.  *    I added 2 #ifdef TIMEVAL to use the System-V timeval package instead.
  76.  *    The Makefile just has a -DTIMEVAL and a magic -U_SYSV_SOURCE to get
  77.  *    around a pre-defined type "ulong" in sys/types.h (thanks to Apple).
  78.  * Did more code overhauling:
  79.  *    add lots more comments, rename variables, reformat source.
  80.  * Put code in un_hqx to avoid suspected buffering problem.
  81.  * -----
  82.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  83.  * (503) 685-1576 (work)
  84.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  85.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  86.  * Version 1.80 15Jul92
  87.  * ----------------------------------------------------------------------------
  88.  
  89.  * ----------------------------------------------------------------------------
  90.  * External
  91.  * -----
  92.  * Made hqx file scan processing much smarter
  93.  *    so, for example, info-mac/comm/qwk-reader.hqx,
  94.  *    complete with extraneous colons in column one, converts correctly
  95.  *    (problem described by Edward John Sabol <es2j+@andrew.cmu.edu>)
  96.  * Avoid silly perror on usage message (prompted by Edward John Sabol)
  97.  * Improve error message regarding improper format
  98.  * Added more caveats to man page
  99.  * -----
  100.  * Internal
  101.  * -----
  102.  * Fixed typo's in printf lines to pass all expected arguments
  103.  *    (pointed out by Bo Holst-Christensen
  104.  *    [holst@diku.dk/dikubhc1@uts.uni-c.dk/holst@login.dkuug.dk])
  105.  * Tweak Makefile to ease shar creation and special case ulong, not A/UX
  106.  * Add yet more comments and debugging code
  107.  * -----
  108.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  109.  * (503) 685-1576 (work)
  110.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  111.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  112.  * Version 1.82 30Jul92
  113.  * ----------------------------------------------------------------------------
  114.  
  115.  * ----------------------------------------------------------------------------
  116.  * External
  117.  * -----
  118.  * relax exactly 64 characters per incoming hqx file, and
  119.  * handle files without trailing newline, so, eg, 
  120.  *    Telnet2.5docs.sit.hqx now converts correctly
  121.  *       (failure reported by Justin Sullivan <justin@f2.facts.uky.edu>)
  122.  *    now also processes info-mac/app/road-map.hqx correctly
  123.  *       (failure reported by Victor Norton<norton@andy.bgsu.edu>)
  124.  * rework the man page for improved clarity and completeness
  125.  * -----
  126.  * Internal
  127.  * -----
  128.  * avoid warning message from gcc on Sequent Balance mainframe
  129.  *    reported by Justin Sullivan <justin@f2.facts.uky.edu>
  130.  * bump max incoming line length to 2048 from 255
  131.  * add mcvert.ps target to Makefile
  132.  * -----
  133.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  134.  * (503) 685-1576 (work)
  135.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  136.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  137.  * Version 1.83 03Aug92
  138.  * ----------------------------------------------------------------------------
  139.  
  140.  * ----------------------------------------------------------------------------
  141.  * External
  142.  * -----
  143.  * Found and fixed problem with byte ordering plaguing users of
  144.  *    Sequent's Balance running DYNIX, and DEC computers.
  145.  *    The error message looked something like
  146.  *       hqx_to_bin_fork: writing nnn too many bytes
  147.  * Avoid generating debugging info if not to be printed -- cut runtime in half!
  148.  * Generalize and incorporate -I (info) option processing provided by
  149.  *    Paul Franklin, Computer Enginnering, Univ. of Calif., Davis CA 95616
  150.  *    pdfranklin@ucdavis.edu.
  151.  * Added heuristic to avoid false matches in mail headers.
  152.  *    problem expertly characterized, solution beta tested, and subsequent
  153.  *    improvement suggested by "Jim (J.) Lattanzi" <lattanzi@bnr.ca>
  154.  *    so segmented comp.binaries.mac files (either multi-file or concatenated
  155.  *    single file) should now convert correctly.
  156.  * Added -H switch to disable heuristic.
  157.  * Document heuristic in man page.
  158.  * Fixed (long-standing) bug which precluded -p option from being recognized
  159.  *    and verified decompressing and unpacking of PIT files working.
  160.  *    Thanks to Dave Clemans for providing me with a version of PackIt.
  161.  * Add the version to the extened Usage message emitted by the program.
  162.  * Tune the syntax of the summary in the program and man page.
  163.  * Cleaned up spelling mistakes in the man page.
  164.  * -----
  165.  * Internal
  166.  * -----
  167.  * Close all open streams --
  168.  *    fix for binfile by Paul Franklin <pdfranklin@ucdavis.edu>
  169.  * Incorporate changes suggested by Barry_Wolman@transarc.com
  170.  *    to mactypes.h and Makefile for support of IBM RS/6000 running AIX 3.2
  171.  *    reformat Makefile to avoid long option lines
  172.  * Identify the right Makefile lines for Irix too
  173.  *    suggested by Jack Repenning (jackr@dblues.wpd.sgi.com)
  174.  * Clean up the stream handling and add mopen/mclose
  175.  *    avoid unnecessary /dev/null opens
  176.  *    all file open/close/read/write are checked for success
  177.  * Lower lint content on SunOS and HP-UX.
  178.  *    avoiding all "sometimes ignored" lint messages.
  179.  * Improve modularity with mopen/mclose/converting routines.
  180.  * Tune debugging output information.
  181.  * Verify that passes smoke tests on DomainOS/SunOS/HP-UX/ULTRIX.
  182.  * Reformat these comments to avoid tabs.
  183.  * -----
  184.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  185.  * (503) 685-1576 (work)
  186.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  187.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  188.  * Version 1.87 25Sep92
  189.  * ----------------------------------------------------------------------------
  190.  
  191.  * ----------------------------------------------------------------------------
  192.  * External
  193.  * -----
  194.  * Add README file which describes how to configure and compile mcvert
  195.  * Handle multiple BinHex4.0 inputs in a single file again (thanks to
  196.  *    <Mark_Larimer@pigeon.cpg.cdc.com> for pointing out this regression)
  197.  * Emit the MacBinary header if verbose (to get create and modify times)
  198.  * -----
  199.  * Internal
  200.  * -----
  201.  * Rename some variables, create mac2unix (time) routine, more comments
  202.  * Keep lint content low
  203.  * Pull unnecessary include of <net/nh.h> -- avoid breaking AIX 3.1
  204.  * Fix = vs == typo dealing with protect bit
  205.  * -----
  206.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  207.  * (503) 685-1576 (work)
  208.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  209.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  210.  * Version 1.88 08Dec92
  211.  * ----------------------------------------------------------------------------
  212.  
  213.  * ----------------------------------------------------------------------------
  214.  * External
  215.  * -----
  216.  * emit input file names when verbose is enabled
  217.  *    (suggested by franklin@eecs.ucdavis.edu)
  218.  * make it easier to build on AT&T 3B2's
  219.  * -----
  220.  * Internal
  221.  * -----
  222.  * provide compile time switch to avoid bzero and bcopy, and use memset
  223.  *    and memcpy instead (pointed out by linger@drystone.attmail.com, and
  224.  *    requested again by Larry S. Staples <attjp4!lss>)
  225.  * update Makefile to include incantations required for AT&T 3B2's
  226.  * fflush all diagnostic output to ensure correct order when output to a file
  227.  * -----
  228.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  229.  * (503) 685-1576 (work)
  230.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  231.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  232.  * Version 1.89 05Jan93
  233.  * ----------------------------------------------------------------------------
  234.  
  235.  * ----------------------------------------------------------------------------
  236.  * External
  237.  * -----
  238.  * incorporate MAC_FILETYPE support provided by <root@genome.stanford.edu>
  239.  * minor edits to man page
  240.  * -----
  241.  * Internal
  242.  * -----
  243.  * update Makefile to simplify incantations required for AT&T 3B2's
  244.  * -----
  245.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  246.  * (503) 685-1576 (work)
  247.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  248.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  249.  * Version 1.90 04Mar93
  250.  * ----------------------------------------------------------------------------
  251.  
  252.  * ----------------------------------------------------------------------------
  253.  * External
  254.  * -----
  255.  * handle -b (both .data and .rsrc at same time) option
  256.  *    ( -> MacBinary suggested by bb@math.ufl.edu)
  257.  * remove anomalous file extension handling
  258.  *    (suggested by bb@math.ufl.edu)
  259.  * regularize MAC_EDITOR (author) and MAC_FILETYPE (file type) handling
  260.  * detect and report important file format errors
  261.  * emit output file name too
  262.  * revise Usage line
  263.  * massively revise man page to reflect changes and generally overhaul
  264.  * -----
  265.  * Internal
  266.  * -----
  267.  * avoid overwriting internal storage (what a chore!)
  268.  *    for example, mcvert -UI *.hqx used to abort with a segmentation violation
  269.  *    symptom reported by franklin@eecs.ucdavis.edu  Thu Sep 24 16:39:21 1992
  270.  * check return values from all getc/putc operations
  271.  * find and fix ancient bug extracting resource fork from MacBinary format
  272.  * identify failing file in EOF error messages
  273.  * clarify and amend distribution and update restrictions
  274.  * expand disclaimer (patterned after INFO-MAC CD-ROM -- Thx, Cliff and Joe!)
  275.  * -----
  276.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  277.  * (503) 685-1576 (work)
  278.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  279.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  280.  * Version 2.00 28Feb93
  281.  * ----------------------------------------------------------------------------
  282.  
  283.  * ----------------------------------------------------------------------------
  284.  * External
  285.  * -----
  286.  * unixify all relevent output files
  287.  *    (replace suspect characters with _)
  288.  * macify all relevent MacBinary files
  289.  *    (replace suspect chars with -, take first 20 and last 11 if > 31 char)
  290.  *    avoiding Mac file name > 31 chars (Rick Zaccone zaccone@bucknell.edu)
  291.  * always emit the Macintosh file name in the converting messages
  292.  *    since the UNIX file names are now provided by default
  293.  * rework man page to bring it more up to date, added OTHER SOURCES section
  294.  * emit data and rsrc len when printing bin header
  295.  * report the input character, not the mapped character, if avail,
  296.  *    else report mapped value as hex
  297.  * add -VV (Very Verbose) option
  298.  * -----
  299.  * Internal
  300.  * -----
  301.  * distribute mcvert.idraw, a postscript file
  302.  *    describing mcvert options and transformations pictorially,
  303.  *    contributed by Brian Bartholomew - bb@math.ufl.edu
  304.  * create and distribute README-conversion file
  305.  * update Makefile and README to make it more obvious how to build mcvert
  306.  *    problem reported by David Micklethwaite <mickles@cherax.super.csiro.au>
  307.  * make s/S/v/V flag processing serially reusable
  308.  * avoid obsolete ftime on HP-UX, SunOS, DomainOS -- default is now -DTIMEVAL
  309.  *    problem reported by smith@sfu.ca (Richard Smith) and
  310.  *    Adam Harris (harris@cs.uchicago.edu)
  311.  * avoid SGI bug regarding unterminated character constant within #ifdef notdef
  312.  *    problem reported by smith@sfu.ca (Richard Smith)
  313.  * clean up some FILE confusion
  314.  * make the man page work well across platforms
  315.  * re-lint on SunOS and HP-UX
  316.  * add various additional comments
  317.  * -----
  318.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  319.  * (503) 685-1576 (work)
  320.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  321.  * Version 2.09 30Jun93
  322.  * ----------------------------------------------------------------------------
  323.  
  324.  * ----------------------------------------------------------------------------
  325.  * External
  326.  * -----
  327.  * -----
  328.  * Internal
  329.  * -----
  330.  * incorporate SCO UNIX requirements into the Makefile
  331.  *    info from Fred Lenk, Camarillo, CA [fredgl@tecnet1.jcte.jcs.mil]
  332.  * backout intermediate XOBJ cleanup and continue to do what works for AT&T 3B2
  333.  *    belated & current thanks to Larry S. Staples [attjpn!lss@attibr.att.com]
  334.  *    for providing and proofing the working recipe for AT&T 3B2
  335.  * change OTHER SOURCES to OTHER PROGRAMS in man page, and mention programs
  336.  *    which run on the Mac, including CompactPro, StuffIt, and BinHex 4.0
  337.  * ship the formatted ASCII version of the man page for those without nroff
  338.  * -----
  339.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  340.  * (503) 685-1576 (work)
  341.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  342.  * Version 2.12 19Jul93
  343.  * ----------------------------------------------------------------------------
  344.  
  345.  * ----------------------------------------------------------------------------
  346.  * External
  347.  * -----
  348.  * -----
  349.  * Internal
  350.  * -----
  351.  * add StuffIt Expander mention to man page
  352.  * suggest using text (-t|-u) if data is text in the data (-d) description
  353.  * incorporate AIX Makefile improvement provided by
  354.  *    DaviD W. Sanderson (dws@ssec.wisc.edu)
  355.  * -----
  356.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  357.  * (503) 685-1576 (work)
  358.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  359.  * Version 2.13 13Sep93
  360.  * ----------------------------------------------------------------------------
  361.  
  362.  * ----------------------------------------------------------------------------
  363.  * External
  364.  * -----
  365.  * add -P (pipe to stdout) option
  366.  *    capability requested by lentz@rossi.astro.nwu.edu (Robert Lentz)
  367.  * have all info messages go to stderr, not stdout, to faciliate -P
  368.  *    (undo converting msg to stdout from version 1.6.5, dated Oct 1991)
  369.  * update man page to indicate changes
  370.  * -----
  371.  * Internal
  372.  * -----
  373.  * fiddle with info message to make it a bit clearer
  374.  * -----
  375.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  376.  * (503) 685-1576 (work)
  377.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  378.  * Version 2.14 10Nov93
  379.  * ----------------------------------------------------------------------------
  380.  
  381.  * ----------------------------------------------------------------------------
  382.  * External
  383.  * -----
  384.  * -----
  385.  * Internal
  386.  * -----
  387.  * ensure all source line lengths are less than 80
  388.  *    after someone or something wrapped the 2.14 archive,
  389.  *    and it would not compile (split a character string literal)
  390.  *    [recall that shar usually prepends X, which bumps the line length by 1]
  391.  *    [also, this makes editing with emacs at 80 columns a bit better]
  392.  * add check_linelen to Makefile to ensure compliance
  393.  * -----
  394.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  395.  * (503) 685-1576 (work)
  396.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  397.  * Version 2.15 15Nov93
  398.  * ----------------------------------------------------------------------------
  399.  
  400.  * ----------------------------------------------------------------------------
  401.  * This program may be freely distributed for non-profit purposes.  It
  402.  * may not be sold, by itself or as part of a collection of software.
  403.  * However, it may be distributed in source form with large
  404.  * collections of freeware and shareware, such as the INFO-MAC CD-ROM,
  405.  * which charge only a modest fee for publishing, but not selling, the
  406.  * software.  It may be freely modified as long as no modified version
  407.  * is independently distributed.  Modifications of interest to all can
  408.  * be incorporated into the program by sending them to me for
  409.  * inclusion and redistribution, or by releasing an updated mcvert to
  410.  * the info-mac archives.  Parts of the code can be used in other
  411.  * programs.  We hope you find mcvert useful, and enjoy using it.
  412.  * ----------------------------------------------------------------------------
  413.  
  414.  * ----------------------------------------------------------------------------
  415.  * DISCLAIMER -- USE mcvert software AT YOUR OWN RISK
  416.  * This mcvert software is provided "as is" without warrantee of any
  417.  * kind.  The entire risk as to the results and performance of the
  418.  * software is assumed by you, and in no event will we be liable for
  419.  * any consequential, incidental, or indirect damages suffered in the
  420.  * course of using this software.
  421.  * ----------------------------------------------------------------------------
  422.  
  423.  * ----------------------------------------------------------------------------
  424.  * Things that yet could be done:
  425.  * ---
  426.  * handle incoming BinHex4.0 files with ^M instead of ^J
  427.  * (requested by jonathan brecher brecher@husc.harvard.edu 29Mar93)
  428.  *  eg, right now we toss the entire line if it doesn't start with a colon
  429.  *  but the start of the line could be later on, following a ^M
  430.  * check for more file format errors, eg, MacBinary must be multiple of 128
  431.  * option to avoid .text extension on write (Rick Zaccone zaccone@bucknell.edu)
  432.  * check return values from fputs/fprintf
  433.  * provide header heuristic tuning option: set length and/or same sensitivity
  434.  * ----------------------------------------------------------------------------
  435.  */
  436.  
  437. /*
  438.  * Naming
  439.  *        DOWNLOAD
  440.  *            => converting TO MacBinary
  441.  *            => direction == FORWARDS
  442.  *            => use un_* routines (un => UNdo encoding?)
  443.  *        UPLOAD
  444.  *            => converting FROM MacBinary
  445.  *            => direction == BACKWARDS
  446.  *            => use re_* routines (re => Really Encode?)
  447.  */
  448.  
  449. #include "mactypes.h"
  450.  
  451. /* it would be natural to use an enum here, but avoid "fancy" features */
  452. #define HQX 0
  453. #define TEXT 1
  454. #define DATA 2
  455. #define RSRC 3
  456. #define BOTH 5
  457.  
  458. #define FORWARDS 0
  459. #define BACKWARDS 1
  460.  
  461. FILE *devnull;
  462. FILE *convert;
  463. FILE *verbose;
  464. FILE *debug;
  465. int   Debug;
  466.  
  467. char **hqxnames, **hqxnames_left;
  468. char *dir, *ext, *mac_auth, *mac_type;
  469. int translate_eol;
  470. char *maxlines_str;
  471. int maxlines;
  472.  
  473. /* used to skip suspect mail header lines */
  474. int suspect_shorter = 12;
  475. int suspect_same = 1;
  476.  
  477. /* used to avoid writing output files */
  478. int info_only;
  479.  
  480. /* pipe appropriate output -- write to stdout */
  481. int pipe_out;
  482.  
  483. char Usage[] = "\
  484. Usage: %s { [option] ... name ...} ...\n\
  485.  version:\t%4.2f\n\
  486.  default:\t-xDqv\n\
  487. \n\
  488.  option:\n\
  489. \t-x\tBinHex        .hqx  <-> MacBinary\n\
  490. \t-u\tText(trans)   .text <-> MacBinary\n\
  491. \t-h\tHost(as is)   .text <-> MacBinary\n\
  492. \t-d\tData          .data <-> MacBinary\n\
  493. \t-r\tResource      .rsrc <-> MacBinary\n\
  494. \t-b\tBoth    .data .rsrc <-> MacBinary\n\
  495. \n\
  496. \t-D\tDownload       Other -> MacBinary\n\
  497. \t-U\tUpload     MacBinary -> Other\n\
  498. \n\
  499. \t-p\tBinHex -> MacBinary => unpack PIT\n\
  500. \t-q\tdisable unpack PIT\n\
  501. \t-t\ttranslate end-of-line chars (useful with -b)\n\
  502. \n\
  503. \t-I\tInformation only (does not write output files)\n\
  504. \t-P\tPipe output to stdout\n\
  505. \t-s\tsilent\n\
  506. \t-S\tSilent about ``Converting ... '' lines too\n\
  507. \t-v\tverbose\n\
  508. \t-V\tVerbose, includes debugging information\n\
  509. \t-VV\tVery Verbose, includes extra debugging information\n\
  510. \t-H\tdisable skip-legal-but-suspect-lines Heuristic\n\
  511. \n\
  512. Environment:\n\
  513. \tMAC_FILETYPE  \tTEXT|????\tMac file type for Text|other\n\
  514. \tMAC_EDITOR    \tMACA|????\tMac creator (author) for Text|other\n\
  515. \tMAC_EXT       \t.bin     \textension for -D\n\
  516. \tMAC_DLOAD_DIR \t.        \tdirectory for -D\n\
  517. \tMAC_LINE_LIMIT\tnone     \tmaximum line length for -Ux\n\
  518. ";
  519.  
  520. char *cmdname;
  521.  
  522. main(argc, argv)
  523.     int argc;
  524.     char **argv;
  525. {
  526.     char *flags, *getenv();
  527.     int direction, mode, unpit_flag;
  528.  
  529.     cmdname = argv[0];
  530.  
  531.     /* Early error and clean exit if missing arguments */
  532.     if (argc < 2) {
  533.         usage();
  534.         /*NOTREACHED*/
  535.     }
  536.  
  537.     devnull = fopen("/dev/null", "w+");
  538.  
  539.     argv++;
  540.     argc--;
  541.  
  542.     convert = stderr;
  543.     verbose = stderr;
  544.     debug = devnull;
  545.     Debug = 0;
  546.  
  547.     direction = FORWARDS;
  548.     mode = HQX;
  549.     unpit_flag = 0;
  550.  
  551.     mac_type = getenv("MAC_FILETYPE");
  552.     mac_auth = getenv("MAC_EDITOR");
  553.  
  554.     if ((ext = getenv("MAC_EXT")) == NULL)
  555.         ext = ".bin";
  556.     if ((dir = getenv("MAC_DLOAD_DIR")) == NULL)
  557.         dir = ".";
  558.     if ((maxlines_str = getenv("MAC_LINE_LIMIT")) == NULL)
  559.         maxlines = 0;
  560.     else {
  561.         maxlines = atoi(maxlines_str);
  562.         if (maxlines < MIN_HQX_LINES) {
  563.             fprintf(stderr, "%s: %s; was %d; reset to %d\n",
  564.                 cmdname,
  565.                 "warning: MAC_LINE_LIMIT too small",
  566.                 maxlines, MIN_HQX_LINES);
  567.             fflush(stderr);
  568.             maxlines = MIN_HQX_LINES;
  569.         }
  570.     }
  571.  
  572.     /* Make command line arguments globally accessible */
  573.     hqxnames = (char **) calloc((unsigned)argc + 1, sizeof(char *));
  574.     hqxnames_left = hqxnames;
  575.     while (argc--)
  576.         *hqxnames_left++ = *argv++;
  577.  
  578.     /* Flag the end of the list */
  579.     *hqxnames_left = "-";
  580.     hqxnames_left = hqxnames;
  581.  
  582.     /* While not at the end of the list */
  583.     while (strcmp(*hqxnames_left, "-")) {
  584.         translate_eol = 0;
  585.         if (hqxnames_left[0][0] == '-') {
  586.             flags = *hqxnames_left++;
  587.             while (*++flags)
  588.                 switch (*flags) {
  589.                 case 'x':
  590.                     mode = HQX;
  591.                     break;
  592.                 case 'u':
  593.                     translate_eol = 1;
  594.                     mode = TEXT;
  595.                     break;
  596.                 case 'd':
  597.                     mode = DATA;
  598.                     break;
  599.                 case 'r':
  600.                     mode = RSRC;
  601.                     break;
  602.                 case 'h':
  603.                     translate_eol = 0;
  604.                     mode = TEXT;
  605.                     break;
  606.                 case 'b':
  607.                     mode = BOTH;
  608.                     break;
  609.                 case 't':
  610.                     translate_eol = 1;
  611.                     break;
  612.                 case 'D':
  613.                     direction = FORWARDS;
  614.                     break;
  615.                 case 'U':
  616.                     direction = BACKWARDS;
  617.                     break;
  618.                 case 'q':
  619.                     unpit_flag = 0;
  620.                     break;
  621.                 case 'p':
  622.                     unpit_flag = 1;
  623.                     break;
  624.                 case 'S':
  625.                     convert = devnull;
  626.                     verbose = devnull;
  627.                     debug = devnull;
  628.                     Debug = 0;
  629.                     break;
  630.                 case 's':
  631.                     convert = stderr;
  632.                     verbose = devnull;
  633.                     debug = devnull;
  634.                     Debug = 0;
  635.                     break;
  636.                 case 'v':
  637.                     convert = stderr;
  638.                     verbose = stderr;
  639.                     debug = devnull;
  640.                     Debug = 0;
  641.                     break;
  642.                 case 'V':
  643.                     convert = stderr;
  644.                     verbose = stderr;
  645.                     debug = stderr;
  646.                     Debug++;
  647.                     break;
  648.                 case'H':
  649.                     suspect_shorter = suspect_same = 0;
  650.                     break;
  651.                 case 'I':
  652.                     info_only = 1;
  653.                     break;
  654.                 case 'P':
  655.                     pipe_out = 1;
  656.                     break;
  657.                 default:
  658.                     usage();
  659.                     /*NOTREACHED*/
  660.                 }
  661.         }
  662.  
  663.         if (direction == BACKWARDS)
  664.             if (mode == HQX && unpit_flag)
  665.                 re_hqx();      /* no re_pit() yet */
  666.             else if (mode == HQX)
  667.                 re_hqx();
  668.             else
  669.                 re_other(mode);
  670.         else if (mode == HQX)
  671.             un_hqx(unpit_flag);
  672.         else
  673.             un_other(mode);
  674.     }
  675.  
  676.     exit(0);
  677.     /*NOTREACHED*/
  678. }
  679.  
  680. /* An array useful for CRC calculations that use 0x1021 as the "seed" */
  681. word magic[] = {
  682.     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  683.     0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  684.     0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  685.     0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  686.     0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  687.     0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  688.     0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  689.     0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  690.     0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  691.     0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  692.     0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  693.     0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  694.     0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  695.     0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  696.     0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  697.     0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  698.     0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  699.     0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  700.     0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  701.     0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  702.     0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  703.     0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  704.     0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  705.     0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  706.     0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  707.     0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  708.     0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  709.     0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  710.     0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  711.     0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  712.     0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  713.     0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  714. };
  715.  
  716.  
  717. /*
  718.  * calc_crc() --
  719.  *   Compute the MacBinary II-style CRC for the data pointed to by p, with the
  720.  *   crc seeded to seed.
  721.  *
  722.  *   Modified by Jim Van Verth to use the magic array for efficiency.
  723.  */
  724. short
  725. calc_mb_crc(p, len, seed)
  726.     unsigned char *p;
  727.     long len;
  728.     short seed;
  729. {
  730.     short hold;        /* crc computed so far */
  731.     long i;            /* index into data */
  732.  
  733.     extern unsigned short magic[];    /* the magic array */
  734.  
  735.     hold = seed;                   /* start with seed */
  736.     for (i = 0; i < len; i++, p++) {
  737.         hold ^= (*p << 8);
  738.         hold = (hold << 8) ^ magic[(unsigned char) (hold >> 8)];
  739.     }
  740.  
  741.     return (hold);
  742. }                /* calc_crc() */
  743.  
  744.  
  745. /* Report a fatal error, and exit, never return */
  746. error(msg, name)
  747.     char msg[], name[];
  748.  
  749. {
  750.     fprintf(stderr, msg, name);
  751.     (void)putc('\n', stderr);
  752.     perror("\nlast perror (may not be relevant)");
  753.     fprintf(stderr, "%s: exiting\n", cmdname);
  754.     if (debug == stderr)
  755.         abort();
  756.     exit(2);
  757.     /*NOTREACHED*/
  758. }
  759.  
  760. /* replace illegal Unix characters in file name */
  761. /* make sure host file name doesn't get truncated beyond recognition */
  762. unixify(np)
  763.     register char *np;
  764. {
  765.     register ulong c;
  766.  
  767.     c = strlen(np);
  768.     if (c > SYSNAMELEN - MAXEXTENSION)
  769.         c = SYSNAMELEN - MAXEXTENSION;
  770.     np[c] = '\0';
  771.  
  772.     /* pre-decrement to match pre-increment within loop */
  773.     np--;
  774.     while (c = *++np)
  775.         /*
  776.          * that is, ``if control or blank, or slash, or delete or 8bit''
  777.          * which is the same as ``if blank, slash, or non graphic''
  778.         */
  779.         if (c <= ' ' || c == '/' || c > '~')
  780.             *np = '_';
  781. }
  782.  
  783. /*
  784.  * replace illegal Macintosh characters in file name
  785.  * return resulting length
  786.  *
  787.  * According to Inside Macintosh, IV-90, valid file names
  788.  *    must be [1..31] characters long
  789.  *    must not contain a colon
  790.  *    must contain only printing characters
  791.  */
  792. macify(name, len, translate)
  793.     char *name;
  794.     int len;
  795.     int translate;
  796. {
  797.     register char *np;
  798.     register ulong c;
  799.     char *s, *t;
  800.     char buffer[SYSNAMELEN];
  801.  
  802.     /* make a copy to ensure null terminated */
  803.     strncpy(buffer, name, len);
  804.     buffer[len] = 0;
  805.     np = buffer;
  806.  
  807.     if (len < 1)
  808.         error("incoming file name is null", "");
  809.     if (len > 31) {
  810.         /* too long, so just take first 20 and last 11 */
  811.         s = np + 20;
  812.         t = np + len - 11;
  813.         while (*s++ = *t++)
  814.             ;
  815.         len = 31;
  816.     }
  817.  
  818.     if (translate) {
  819.         /* pre-decrement to match pre-increment within loop */
  820.         np--;
  821.         while (c = *++np)
  822.  
  823.          /*
  824.  
  825.           * Inside Macintosh, I-246, defines the printable characters
  826.           * for the Macintosh as 0x20 thru 0xD8 less 0x7F.  Yet, the
  827.           * apple character is above this range, at 0xF0, and the diamond
  828.           * character is below this range, at 0x13.  And Adobe Garamond
  829.           * has lots of characgters above 0xD8.
  830.           * 
  831.           * On the other hand, we only translate when processing UNIX
  832.           * file names which are usually ASCII, and ASCII printables are
  833.           * ' ' <= valid <= '~'.
  834.           * 
  835.           * But we want to avoid zapping any characters with the high
  836.           * order bit set, so 8 bit character users are not zinged.  I've
  837.           * never used a SONY-NeWS box, but this one's for you.  But how
  838.           * do we know if/that the UNIX and Macintosh extended characters
  839.           * are the same?
  840.           * 
  841.           * So what to do, what to do?
  842.           * 
  843.           * Let's look at it this way: if the UNIX file name has extended
  844.           * characters in it, they got there for a reason, hopefully on
  845.           * purpose, and we'll not gratuitously modify them.
  846.           * 
  847.           * And it looks like the 6.1.5 Finder running with the 6.0.5
  848.           * System will translate both control characters and colon into
  849.           * a dash, but leave the others alone, so so will we.
  850.           * 
  851.           * I don't know if MacOS, as opposed to the Finder, behaves
  852.           * differently, and it's too late tonight to find out.  Maybe
  853.           * some other time.
  854.  
  855.       */
  856.  
  857.           if (c < ' ' || c == ':' || c == '\177')
  858.                  *np = '-';
  859.     }
  860.  
  861.     /* copy the resulting string back in place */
  862.     strncpy(name, buffer, len);
  863.  
  864.     return len;
  865. }
  866.  
  867. /*
  868.  * Unix time (GMT since 1-1-1970)
  869.  * Mac time (local since 1-1-1904)
  870.  */
  871. #define MACTIMEDIFF 0x7c25b080    /* Mac time of 00:00:00 GMT, Jan 1, 1970 */
  872.  
  873. /* Convert Unix time to Mac time */
  874. ulong
  875. unix2mac(xtime)
  876.     ulong xtime;
  877. {
  878. #ifdef TIMEVAL
  879.     struct timeval t;
  880.     struct timezone tz;
  881.  
  882.     gettimeofday(&t, &tz);
  883.     return long2mac(xtime + MACTIMEDIFF
  884.        - 60 * (tz.tz_minuteswest - 60 * tz.tz_dsttime));
  885. #else
  886.     struct timeb tp;
  887.  
  888.     ftime(&tp);
  889.     return long2mac(xtime + MACTIMEDIFF
  890.         - 60 * (tp.timezone - 60 * tp.dstflag));
  891. #endif
  892. }
  893.  
  894. /* Convert Mac time to Unix time */
  895. ulong
  896. mac2unix(xtime)
  897.     ulong xtime;
  898. {
  899. #ifdef TIMEVAL
  900.     struct timeval t;
  901.     struct timezone tz;
  902.  
  903.     gettimeofday(&t, &tz);
  904.     return (mac2long(xtime) - MACTIMEDIFF
  905.        + 60 * (tz.tz_minuteswest - 60 * tz.tz_dsttime));
  906. #else
  907.     struct timeb tp;
  908.  
  909.     ftime(&tp);
  910.     return (mac2long(xtime) - MACTIMEDIFF
  911.         + 60 * (tp.timezone - 60 * tp.dstflag));
  912. #endif
  913. }
  914.  
  915. /*
  916.  * computes the appropriate output files
  917.  * and the appropriate optional suffixes,
  918.  * all depending on the processing "mode"
  919.  */
  920.  
  921. mode_to_fname_suffix(
  922.     mode, base_fname, data_fname, data_suffix, rsrc_fname, rsrc_suffix
  923. )
  924.     int mode;
  925.     char *base_fname, *data_fname, **data_suffix, *rsrc_fname, **rsrc_suffix;
  926. {
  927.  
  928.     /* clear names to indicate nothing selected yet */
  929.     *data_fname = *rsrc_fname = 0;
  930.  
  931.     switch (mode) {
  932.     case TEXT:
  933.         strcpy(data_fname, base_fname);
  934.         *data_suffix = ".text";
  935.         break;
  936.     case DATA:
  937.         strcpy(data_fname, base_fname);
  938.         *data_suffix = ".data";
  939.         break;
  940.     case RSRC:
  941.         strcpy(rsrc_fname, base_fname);
  942.         *rsrc_suffix = ".rsrc";
  943.         break;
  944.     case BOTH:
  945.         strcpy(data_fname, base_fname);
  946.         strcat(data_fname, ".data");
  947.         *data_suffix = "";
  948.         strcpy(rsrc_fname, base_fname);
  949.         strcat(rsrc_fname, ".rsrc");
  950.         *rsrc_suffix = "";
  951.         break;
  952.     default:
  953.         error("Internal error: unexpected mode", "");
  954.         break;
  955.     }
  956. }
  957.  
  958. /*
  959.     This procedure basically copies the input file(s) to the output
  960.     MacBinary file; in TEXT (translate_eol) mode it changes LF's to
  961.     CR's, and in any mode it forges a Mac info header.  Author type
  962.     comes from the MAC_EDITOR environment variable if it is defined.
  963.  */
  964.  
  965. un_other(mode)
  966.     int mode;
  967. {
  968.     char data_fname[SYSNAMELEN], rsrc_fname[SYSNAMELEN], binfname[SYSNAMELEN];
  969.     FILE *data_file, *rsrc_file, *binfile;
  970.     char *base_fname, *data_suffix, *rsrc_suffix;
  971.     struct stat data_stbuf, rsrc_stbuf;
  972.     ulong dlen, rlen, mtim, ctim;
  973.  
  974.     info_header info;
  975.     register ulong b;                /* not character, must hold EOF diagnostic */
  976.     register ulong nchars;
  977.     int extra_chars;
  978.     short crc, calc_mb_crc();
  979.     long len;
  980.  
  981.     while (hqxnames_left[0][0] != '-') {
  982.  
  983.         if (strlen(*hqxnames_left) >= SYSNAMELEN)
  984.             error("Error: specified base file name is too long", "");
  985.         base_fname = *hqxnames_left++;
  986.  
  987.         /* set up file names */
  988.         mode_to_fname_suffix(mode, base_fname,
  989.             data_fname, &data_suffix, rsrc_fname, &rsrc_suffix);
  990.  
  991.         /* process the data file, if requested */
  992.         dlen = 0;
  993.         if (*data_fname) {
  994.             data_file = mopen(data_fname, data_suffix, "r");
  995.             if (fstat(fileno(data_file), &data_stbuf))
  996.                 error("Cannot stat %s", data_fname);
  997.             mtim = unix2mac((ulong)data_stbuf.st_mtime);
  998.             ctim = unix2mac((ulong)data_stbuf.st_ctime);
  999.             dlen = long2mac(data_stbuf.st_size);
  1000.         }
  1001.  
  1002.         /* process the rsrc file, if requested */
  1003.         rlen = 0;
  1004.         if (*rsrc_fname) {
  1005.             rsrc_file = mopen(rsrc_fname, rsrc_suffix, "r");
  1006.             if (fstat(fileno(rsrc_file), &rsrc_stbuf))
  1007.                 error("Cannot stat %s", rsrc_fname);
  1008.             mtim = unix2mac((ulong)rsrc_stbuf.st_mtime);
  1009.             ctim = unix2mac((ulong)rsrc_stbuf.st_ctime);
  1010.             rlen = long2mac(rsrc_stbuf.st_size);
  1011.         }
  1012.  
  1013.         /* stuff header data into the info header */
  1014.  
  1015.         bzero((char*)&info, sizeof(info_header));
  1016.  
  1017.         info.nlen = strlen(base_fname);
  1018.         info.nlen = (info.nlen > NAMELEN) ? NAMELEN : info.nlen;
  1019.         strncpy((char*)info.name, base_fname, (int)info.nlen);
  1020.  
  1021.         /* now make sure the resulting name is valid */
  1022.         info.nlen = macify((char*)info.name, (int)info.nlen, 1);
  1023.  
  1024.         info.uploadvers = '\201';
  1025.         info.readvers = '\201';
  1026.  
  1027.         bcopy((char*)&mtim, (char*)info.mtim, 4);
  1028.         bcopy((char*)&ctim, (char*)info.ctim, 4);
  1029.  
  1030.         bcopy((char*)&dlen, (char*)info.dlen, 4);
  1031.         bcopy((char*)&rlen, (char*)info.rlen, 4);
  1032.  
  1033.         switch (mode) {
  1034.         case TEXT:
  1035.             bcopy(mac_type ? mac_type : "TEXT", (char*)info.type, 4);
  1036.             bcopy(mac_auth ? mac_auth : "MACA", (char*)info.auth, 4);
  1037.             break;
  1038.         case DATA:
  1039.         case RSRC:
  1040.         case BOTH:
  1041.             bcopy(mac_type ? mac_type : "????", (char*)info.type, 4);
  1042.             bcopy(mac_auth ? mac_auth : "????", (char*)info.auth, 4);
  1043.             break;
  1044.         default:
  1045.             error("Internal error: unexpected mode", "");
  1046.             break;
  1047.         }
  1048.  
  1049.         /* calculate CRC */
  1050.         crc = calc_mb_crc((unsigned char*)&info, 124L, 0);
  1051.         info.crc[0] = (char) (crc >> 8);
  1052.         info.crc[1] = (char) crc;
  1053.  
  1054.         /* Create the .bin file and write the info to it */
  1055.  
  1056.         len = strlen(dir) + strlen(base_fname) + strlen(ext) + 1;
  1057.         if (len >= sizeof(binfname))
  1058.             error("Error: generated binfname would be too long", "");
  1059.         /*
  1060.          * base_fname does not need to be unixified --
  1061.          * was valid coming in
  1062.          */
  1063.         sprintf(binfname, "%s/%s%s", dir, base_fname, ext);
  1064.         binfile = mopen(binfname, "", "w");
  1065.  
  1066.         converting(info.name, (int)info.nlen, info.type, info.auth);
  1067.         print_bin_hdr("Creating", &info);
  1068.         if (1 != fwrite((char*)&info, sizeof(info), 1, binfile))
  1069.             error("fwrite failed on binfile", "");
  1070.  
  1071.         /* pump out the data portion */
  1072.         if (*data_fname) {
  1073.             nchars = data_stbuf.st_size;
  1074.             extra_chars = 127 - (nchars + 127) % 128;
  1075.  
  1076.             if (translate_eol)
  1077.                 while (nchars--) {
  1078.                     (b = getc(data_file)) == EOF &&
  1079.                         error("Error: getc failed on data_file", "");
  1080.                     if (b == LF)
  1081.                         b = CR;
  1082.                     putc((char)b, binfile) == EOF &&
  1083.                         error("Error: putc failed on binfile", "");
  1084.                 }
  1085.  
  1086.             else
  1087.                 while (nchars--) {
  1088.                     (b = getc(data_file)) == EOF &&
  1089.                         error("Error: getc failed on data_file", "");
  1090.                     putc((char)b, binfile) == EOF &&
  1091.                         error("Error: putc failed on binfile", "");
  1092.                 }
  1093.  
  1094.             while (extra_chars--) {
  1095.                 putc(0, binfile) == EOF &&
  1096.                     error("Error: putc failed on binfile", "");
  1097.             }
  1098.  
  1099.             mclose(&data_file, "txtfile");
  1100.         }
  1101.  
  1102.         /* pump out the rsrc portion */
  1103.         if (*rsrc_fname) {
  1104.             nchars = rsrc_stbuf.st_size;
  1105.             extra_chars = 127 - (nchars + 127) % 128;
  1106.  
  1107.             while (nchars--) {
  1108.                 (b = getc(rsrc_file)) == EOF &&
  1109.                     error("Error: getc failed on rsrc_file", "");
  1110.                 putc((char)b, binfile) == EOF &&
  1111.                     error("Error: putc failed on binfile", "");
  1112.             }
  1113.  
  1114.             while (extra_chars--) {
  1115.                 putc(0, binfile) == EOF &&
  1116.                     error("Error: putc failed on binfile", "");
  1117.             }
  1118.  
  1119.             mclose(&rsrc_file, "txtfile");
  1120.         }
  1121.  
  1122.         mclose(&binfile, "binfile");
  1123.     }
  1124. }
  1125.  
  1126. /*
  1127.     This procedure basically copies the MacBinary input file to the
  1128.     output file(s); in TEXT (translate_eol) mode it changes CR's to
  1129.     LF's, and in any mode it skikps over the Mac info header.
  1130.  */
  1131.  
  1132. re_other(mode)
  1133.     int mode;
  1134. {
  1135.     char base_fname[SYSNAMELEN];
  1136.     char data_fname[SYSNAMELEN], rsrc_fname[SYSNAMELEN], binfname[SYSNAMELEN];
  1137.     FILE *data_file, *rsrc_file, *binfile;
  1138.     char *data_suffix, *rsrc_suffix;
  1139.  
  1140.     info_header info;
  1141.     register ulong b;
  1142.     register ulong nchars;
  1143.     ulong temp;
  1144.     int extra_chars;
  1145.     long len;
  1146.  
  1147.     while (hqxnames_left[0][0] != '-') {
  1148.  
  1149.         /* suck in the MacBinary header */
  1150.         if (strlen(*hqxnames_left) >= sizeof(binfname))
  1151.             error("Error: specified binfname is too long", "");
  1152.         strcpy(binfname, *hqxnames_left++);
  1153.         binfile = mopen(binfname, ext, "r");
  1154.         if (1 != fread((char*)&info, sizeof(info), 1, binfile))
  1155.             error("fread failed on binfile", "");
  1156.  
  1157.         /* figure out the target base file name */
  1158.         if (info.nlen >= NAMELEN)
  1159.             error("Error: corrupt BinHex data format", "");
  1160.         strncpy(base_fname, (char*)info.name, (int)info.nlen);
  1161.         base_fname[info.nlen] = '\0';
  1162.         converting(info.name, (int)info.nlen, info.type, info.auth);
  1163.         print_bin_hdr("Reading", &info);
  1164.  
  1165.         /* ensure the output files have no bogus UNIX characters */
  1166.         unixify(base_fname);
  1167.         mode_to_fname_suffix(mode, base_fname,
  1168.             data_fname, &data_suffix, rsrc_fname, &rsrc_suffix);
  1169.  
  1170.         /* always use suffix on write */
  1171.  
  1172.         if (*data_fname) {
  1173.             len = strlen(data_fname) + strlen(data_suffix);
  1174.             if (len >= sizeof(data_fname))
  1175.                 error("Error: generated data_fname would be too long", "");
  1176.             strcat(data_fname, data_suffix);
  1177.             data_file = mopen(data_fname, "", "w");
  1178.         }
  1179.  
  1180.         if (*rsrc_fname) {
  1181.             len = strlen(rsrc_fname) + strlen(rsrc_suffix);
  1182.             if (len >= sizeof(rsrc_fname))
  1183.                 error("Error: generated rsrc_fname would be too long", "");
  1184.             strcat(rsrc_fname, rsrc_suffix);
  1185.             rsrc_file = mopen(rsrc_fname, "", "w");
  1186.         }
  1187.  
  1188.         /* process the data fork */
  1189.         bcopy((char*)info.dlen, (char *) &temp, 4);
  1190.         nchars = temp;
  1191.         extra_chars = 127 - (nchars + 127) % 128;
  1192.  
  1193.         if (*data_fname) {
  1194.  
  1195.             if (translate_eol)
  1196.                 while (nchars--) {
  1197.                     (b = getc(binfile)) == EOF &&
  1198.                         error("Error: getc failed on binfile", "");
  1199.                     if (b == CR)
  1200.                         b = LF;
  1201.                     putc((char)b, data_file) == EOF &&
  1202.                         error("Error: putc failed on data_file", "");
  1203.                 }
  1204.  
  1205.             else
  1206.                 while (nchars--) {
  1207.                     (b = getc(binfile)) == EOF &&
  1208.                         error("Error: getc failed on binfile", "");
  1209.                     putc((char)b, data_file) == EOF &&
  1210.                         error("Error: putc failed on data_file", "");
  1211.                 }
  1212.         
  1213.             mclose(&data_file, data_fname);
  1214.  
  1215.         } else {
  1216.  
  1217.             /* skip the actual data */
  1218.             while (nchars--) {
  1219.                 getc(binfile) == EOF &&
  1220.                     error("Error: getc failed on binfile", "");
  1221.             }
  1222.         }
  1223.  
  1224.         /* eat the padding to 128 byte boundary */
  1225.         while (extra_chars--) {
  1226.             getc(binfile) == EOF &&
  1227.                 error("Error: getc failed on binfile", "");
  1228.         }
  1229.  
  1230.         /* process the rsrc fork */
  1231.  
  1232.         bcopy((char*)info.rlen, (char *) &temp, 4);
  1233.         nchars = temp;
  1234.  
  1235.         if (*rsrc_fname) {
  1236.             while (nchars--) {
  1237.                 (b = getc(binfile)) == EOF &&
  1238.                     error("Error: getc failed on binfile", "");
  1239.                 putc((char)b, rsrc_file) == EOF &&
  1240.                     error("Error: putc failed on rsrc_file", "");
  1241.             }
  1242.             mclose(&rsrc_file, rsrc_fname);
  1243.         }
  1244.  
  1245.         mclose(&binfile, "binfile");
  1246.     }
  1247. }
  1248.  
  1249. usage()
  1250. {
  1251.     fprintf(stderr, Usage, cmdname, VERSION/100.);
  1252.     exit(1);
  1253.     /*NOTREACHED*/
  1254. }
  1255.  
  1256.  
  1257. /* My FileIO routines, to enable clean implementation of info_only */
  1258. /* If info_only and open for write, inform user and use devnull instead */
  1259. /* If pipe_out and open for write, inform user and use stdout instead */
  1260. /* If trying to close devnull or stdout, don't */
  1261.  
  1262. FILE *
  1263. mopen(name, exten, type)
  1264.     char *name;
  1265.     char *exten;
  1266.     char *type;
  1267. {
  1268.     FILE *result;
  1269.  
  1270.     switch (*type) {
  1271.     case 'r':
  1272.         result = fopen(name, type);
  1273.         if (result == NULL && *exten) {
  1274.             /* see if adding the extension would help */
  1275.             int len_root = strlen(name);
  1276.             int len_ext = strlen(exten);
  1277.             char *dotspot = name + len_root - len_ext;
  1278.             if (strcmp(exten, dotspot)) {
  1279.                 if (len_root + len_ext >= SYSNAMELEN)
  1280.                     error("Error: generated file name would be too long", "");
  1281.                 strcat(name, exten);
  1282.                 result = fopen(name, type);
  1283.             }
  1284.         }
  1285.         if (result == NULL)
  1286.             error("Cannot open %s for read", name);
  1287.         else {
  1288.             fprintf(verbose, "\n%-15s%-27s\n",
  1289.                 "Input file", name);
  1290.             fflush(verbose);
  1291.         }
  1292.         break;
  1293.  
  1294.     case 'w':
  1295.         if (info_only) {
  1296.             fprintf(verbose, " %-14s%-27s -I (info only) specified\n",
  1297.                 " No output to", name);
  1298.             fflush(verbose);
  1299.             result = devnull;
  1300.         } else if (pipe_out) {
  1301.             fprintf(verbose, " %-14s%-27s -P (pipe to stdout) specified\n",
  1302.                 " stdout, vice", name);
  1303.             fflush(verbose);
  1304.             result = stdout;
  1305.         } else {
  1306.             result = fopen(name, type);
  1307.             if (result == NULL)
  1308.                 error("Cannot open %s for write", name);
  1309.             else {
  1310.                 fprintf(verbose, "%-15s%-27s\n",
  1311.                     "Output file", name);
  1312.                 fflush(verbose);
  1313.             }
  1314.         }
  1315.         break;
  1316.  
  1317.     default:
  1318.         fprintf(stderr, "%s: internal error in mopen -- exiting\n", cmdname);
  1319.         exit(2);
  1320.  
  1321.     }
  1322.  
  1323.     return result;
  1324. }
  1325.  
  1326. mclose(stream_p, name)
  1327.     FILE **stream_p;
  1328.     char *name;
  1329. {
  1330.     if (*stream_p && *stream_p != devnull && *stream_p != stdout) {
  1331.         if (fclose(*stream_p) == EOF)
  1332.             error("Error closing %s", name);
  1333.     }
  1334.  
  1335.     *stream_p = (FILE *)0;
  1336. }
  1337.  
  1338. converting(name, len, type, auth)
  1339.     byte *name;
  1340.     int len;
  1341.     byte *type;
  1342.     byte *auth;
  1343. {
  1344.     char buffer[SYSNAMELEN];
  1345.  
  1346.     /* make a copy to ensure null terminated */
  1347.     strncpy(buffer, (char*)name, len);
  1348.     buffer[len] = 0;
  1349.  
  1350.     fprintf(convert,
  1351.         "%-15s%-30s type = \"%4.4s\", author = \"%4.4s\"\n",
  1352.          info_only ? "Inspecting" : "Converting",
  1353.          buffer, type, auth);
  1354.     fflush(convert);
  1355. }
  1356.  
  1357. print_bin_hdr(msg, hdr)
  1358.     char *msg;
  1359.     info_header *hdr;
  1360. {
  1361.     ulong otime, xtime;
  1362.     long    dlen, rlen;
  1363.     bcopy((char*)hdr->ctim, (char*)&otime, 4);
  1364.     DEBUG && fprintf(debug,
  1365.         "DEBUG: verifying mac2unix/unix2mac: 0x%8lx, 0x%8lx\n",
  1366.         otime, unix2mac(mac2unix(otime)));
  1367.  
  1368.     bcopy((char*)hdr->dlen, (char*)&dlen, 4);
  1369.     bcopy((char*)hdr->rlen, (char*)&rlen, 4);
  1370.  
  1371.     DEBUG && fprintf(debug, "\
  1372. %s\n\
  1373. \tName     %.*s\n\
  1374. \tType     %.4s\n\
  1375. \tCreator  %.4s\n\
  1376. \tDataLen  %ld\n\
  1377. \tRsrcLen  %ld\n\
  1378. ",
  1379.         msg,
  1380.         hdr->nlen, hdr->name,
  1381.         hdr->type,
  1382.         hdr->auth,
  1383.         mac2long(dlen),
  1384.         mac2long(rlen),
  1385.         0);
  1386.  
  1387.     bcopy((char*)hdr->ctim, (char*)&otime, 4);
  1388.     xtime = mac2unix(otime);
  1389.     DEBUG && fprintf(debug, "raw ctim: 0x%8lx, mac2unix: 0x%8lx\n",
  1390.         otime, xtime);
  1391.     DEBUG && fflush(debug);
  1392.  
  1393.     fprintf(verbose, "\tCreated  %s", ctime((time_t *)&xtime));
  1394.     fflush(verbose);
  1395.  
  1396.     bcopy((char*)hdr->mtim, (char*)&otime, 4);
  1397.     xtime = mac2unix(otime);
  1398.     DEBUG && fprintf(debug, "raw mtim: 0x%8lx, mac2unix: 0x%8lx\n",
  1399.         otime, xtime);
  1400.     DEBUG && fflush(debug);
  1401.     fprintf(verbose, "\tModified %s", ctime((time_t *)&xtime));
  1402.     fflush(verbose);
  1403.  
  1404. }
  1405.